package com.softserve.academy.food.services.admin;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.softserve.academy.food.dao.HibernateDishType;
import com.softserve.academy.food.entity.eDish;
import com.softserve.academy.food.entity.eDishType;
import com.softserve.academy.food.model.mDish;
import com.softserve.academy.food.model.mDishType;
import com.softserve.academy.food.services.main.dishService;
import com.softserve.academy.food.services.supplement.Convector;

@Service
public class dishManagerService extends dishService implements iDishManagerService
{
	@Autowired
	private HibernateDishType daoType;
	
	private String root_dir;

	public String getRoot_dir() 
	{
		return root_dir;
	}

	@Override
	public void setRoot_dir( String root_dir ) 
	{
		this.root_dir = root_dir;
	}
	
	@Override
    @Transactional
	public ArrayList<mDish> getDishs()
	{
		ArrayList<mDish> list = new ArrayList<mDish>();
		
		ArrayList<eDish> elist = daoDish.ReadAll();
		
		for (eDish dish : elist)
		{
			list.add( new mDish( dish ) );
		}
		return list;
	}

	@Override
    @Transactional
	public void addDish(mDish dish, MultipartFile image) 
	{
		eDish edish = new eDish( dish );
		
		if (dish!=null)
		{
			
			edish = daoDish.add( edish );
			
			if (image!=null)
			{
				try 
				{
					image.transferTo( new File( root_dir+"dishsimage/"+edish.getId()+".png" ));
					edish.setImage( "dishsimage/"+edish.getId()+".png" );
					daoDish.Update(edish);
				} 
				catch (IllegalStateException | IOException e) 
				{
					e.printStackTrace();
				}
			}
		}
    }

	@Override
	@Transactional
	public mDish getDish( String id )
	{
		return new mDish( daoDish.Read( Convector.StringToInt(id) ) );
	}
	
	@Override
	@Transactional
	public void updateDish(mDish dish) 
	{
		if (dish!=null)
		{
			eDish oldDish = daoDish.Read( dish.getId() );
			
			oldDish.setName( dish.getName() );
			oldDish.setDescription( dish.getDescription() );
			oldDish.setWeight( dish.getWeight() );
			oldDish.setPrice( dish.getPrice() );
			oldDish.setType( dish.getType() );
			
			daoDish.Update( oldDish );
		}
	}
	
	@Override
	@Transactional
	public void delDish( String id )
	{
		File file = new File( root_dir +daoDish.Read( Convector.StringToInt(id) ).getImage() );
		file.delete();
		
		daoDish.Delete( Convector.StringToInt(id) );
	}

	@Override
	@Transactional
	public ArrayList<mDishType> getDishTypes() 
	{
		ArrayList<mDishType> list = new ArrayList<mDishType>();
		
		ArrayList<eDishType> elist = daoType.ReadAll();
		
		for (eDishType type : elist)
		{
			list.add( new mDishType( type ) );
		}
		return list;
	}

	@Override
	@Transactional
	public void addDishType(mDishType type) 
	{
		if (type!=null)
		{
			daoType.Create( new eDishType( type ) );
		}	
	}
	
	@Override
	@Transactional
	public void delDishType( String id ) 
	{
		daoType.Delete( Convector.StringToInt(id) );
	}
	

	
}
